package com.zxl.grocery.microservice.common.rest;

import com.zxl.grocery.microservice.common.biz.BaseBiz;
import com.zxl.grocery.microservice.common.msg.ObjectRestResponse;
import com.zxl.grocery.microservice.common.msg.TableResultResponse;
import com.zxl.grocery.microservice.common.util.Query;
//import com.zxl.grocery.microservice.common.vo.ObjectRestResponse;
//import com.zxl.grocery.microservice.common.vo.TableResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: zxl
 * @Description:
 * @Date: Created in 17:19 2017/10/12
 * @Modified By:
 */
public class BaseController<Biz extends BaseBiz,Entity> {

    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected Biz baseBiz;

    /**
     * @Author: Lzx
     * @Description: 增
     * @Params:  * @param null
     * @Date: 0:55 2017/10/18
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Entity> add(@RequestBody Entity entity){
        baseBiz.insertSelective(entity);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    /**
     * @Author: Lzx
     * @Description: 查 按id
     * @Params:  * @param null
     * @Date: 0:56 2017/10/18
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<Entity> get(@PathVariable int id){
        return new ObjectRestResponse<Entity>().rel(true).data(baseBiz.selectById(id));
    }

    /**
     * @Author: Lzx
     * @Description: 改
     * @Params:  * @param null
     * @Date: 0:55 2017/10/18
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<Entity> update(@RequestBody Entity entity){
        baseBiz.updateSelectiveById(entity);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    /**
     * @Author: Lzx
     * @Description: 删
     * @Params:  * @param null
     * @Date: 0:55 2017/10/18
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<Entity> remove(@PathVariable int id){
        baseBiz.deleteById(id);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    /**
     * @Author: Lzx
     * @Description: 获取全部数据
     * @Params:  * @param null
     * @Date: 0:56 2017/10/18
     */
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
    public List<Entity> all(){
        return baseBiz.selectListAll();
    }

    /**
     * @Author: Lzx
     * @Description: 分页
     * @Params:  * @param 参数
     * @Date: 0:57 2017/10/18
     */
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<Entity> list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        return baseBiz.selectByQuery(query);
    }

    /**
     * @Author: Lzx
     * @Description: 获取当前用户信息
     * @Params:  * @param 参数
     * @Date: 0:57 2017/10/23
     */
    public String getCurrentUserName(){
        String authorization = request.getHeader("Authorization");
        return new String(Base64Utils.decodeFromString(authorization));
    }
}
